package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.warning.ApiLog;
import com.gxar.quick.ar.server.base.facade.ArProtocolConvertFacade;
import com.gxar.quick.ar.server.base.model.protocol.CommonInputWorkInfo;
import com.gxar.quick.ar.server.base.model.protocol.vo.request.QueryPreviewWorkRequest;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksPreviewCreateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksPreviewCreateResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksPreviewInfoResponse;
import com.gxar.quick.ar.server.db.service.WorkPreviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 作品接口
 *
 * @author linsy
 * @version 1.0
 * @created 2023/11/3 15:44
 */
@Api(tags = "作品预览接口")
@RestController
@RequestMapping("/api/works/preview")
@RequiredArgsConstructor
@Slf4j
public class WorksPreviewController {

    private final WorkPreviewService workPreviewService;

    private final ArProtocolConvertFacade arProtocolConvertFacade;

    @ApiOperation("创建预览")
    @PostMapping("/create")
    @ApiLog
    public Result<WorksPreviewCreateResponse> createPreview(@RequestBody @Valid WorksPreviewCreateRequest worksPreviewCreateRequest) {
        UserProfile userProfile = LoginUserThread.get();
        WorksPreviewCreateResponse worksPreviewCreateResponse = workPreviewService.createPreview(userProfile, worksPreviewCreateRequest);
        return Result.success(worksPreviewCreateResponse);
    }

    @ApiOperation("查看预览详情")
    @ApiImplicitParam(name = "id", value = "作品预览id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{id}")
    public Result<WorksPreviewInfoResponse> getPreviewDetail(@PathVariable(name = "id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return workPreviewService.getPreviewDetail(userProfile, id);
    }

    @ApiOperation("查看预览详情 - 经协议转换")
    @PostMapping("/v2")
    public Result<WorksPreviewInfoResponse> getPreviewDetailV2(@RequestBody @Valid QueryPreviewWorkRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        Result<WorksPreviewInfoResponse> response = workPreviewService.getPreviewDetail(userProfile, request.getId());
        if (request.isNeedConvertProtocol() && Objects.nonNull(response.getData())) {
            CommonInputWorkInfo commonWorkInfo = CommonInputWorkInfo.from(response.getData());
            if (Objects.nonNull(commonWorkInfo) && Objects.isNull(commonWorkInfo.getWorksId())) {
                // 补充worksId
                commonWorkInfo.setWorksId(response.getData().getWorkPreviewId());
            }
            arProtocolConvertFacade.convertProtocolConfiguration(commonWorkInfo, request);
            response.getData().setIdentify(commonWorkInfo.getIdentify());
            response.getData().setContent(commonWorkInfo.getContent());
        }
        return response;
    }

}
